<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Nindex extends Base{
    //主题色
    public function themecolor(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $themecolor['template3']=$customer['template3'];
            if($customer['themefont']==1){  
                $themecolor['themefont']='#000';
            }elseif($customer['themefont']==2){
                $themecolor['themefont']='#fff';
            }
            $themecolor['themecolor']=$customer['themecolor'];
            $themecolor['backcolor']=$customer['backcolor'];
            $themecolor['bottomcolor']=$customer['bottomcolor'];
            return json(['code' => 200, 'msg' => '成功！','data'=>$themecolor]);exit();
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    /**
    *小程序部分
    */
    private function httpRequest($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    //openID
    public function openid(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.code"))){
            $code=input("param.code");
        }else{
            return json(['code' => 503, 'msg' => '请给code']);exit();
        }
        $taboutus=Db::name('naboutus')->where('cus_id',$cus_id)->find();
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=".$taboutus['appid']."&secret=".$taboutus['appsecret']."&js_code=".$code."&grant_type=authorization_code";
        $res=$this->httpRequest($url);
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>json_decode($res)]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //如果存在openid不需要保存授权和保存用户信息
    public function isopenid(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        $res=Db::name('users')->where('cus_id',$cus_id)->where('openid',$openid)->find();
        if($res){
            return json(['code' => 200, 'msg' => '存在！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '不存在！']);exit();
        }
    }
    //保存用户信息
    public function userinfoin(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.avatar"))){
            $avatar=input("param.avatar");
        }else{
            return json(['code' => 504, 'msg' => '请给头像']);exit();
        }
        if(!empty(input("param.nickname"))){
            $nickname=input("param.nickname");
        }else{
            return json(['code' => 505, 'msg' => '请给昵称']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $user=Db::name('users')->where('cus_id',$cus_id)->where('openid',$openid)->find();
                if($user){
                    return json(['code' => 201, 'msg' => '用户已存在！']);exit();
                }else{
                    $data['cus_id']=$cus_id;
                    $data['openid']=$openid;
                    $data['avatar']=$avatar;
                    $data['nickname']=$nickname;
                    $data['time']=time();
                    $userin=Db::name('users')->insert($data);
                    if($userin){
                        return json(['code' => 200, 'msg' => '成功保存用户！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '保存失败！']);exit();
                    }
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $user=Db::name('users')->where('cus_id',$cus_id)->where('openid',$openid)->find();
                if($user){
                    return json(['code' => 201, 'msg' => '用户已存在！']);exit();
                }else{
                    $data['cus_id']=$cus_id;
                    $data['openid']=$openid;
                    $data['avatar']=$avatar;
                    $data['nickname']=$nickname;
                    $data['time']=time();
                    $userin=Db::name('users')->insert($data);
                    if($userin){
                        return json(['code' => 200, 'msg' => '成功保存用户！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '保存失败！']);exit();
                    }
                }
            }else{
                return json(['code' => 506, 'msg' => '未开启此平台！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //openID
    public function bdopenid(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.code"))){
            $code=input("param.code");
        }else{
            return json(['code' => 503, 'msg' => '请给code']);exit();
        }
        $taboutus=Db::name('naboutus')->where('cus_id',$cus_id)->find();
        $url="https://spapi.baidu.com/oauth/jscode2sessionkey";
        $post_data['code']=$code;
        $post_data['client_id']=$taboutus['bdappkey'];
        $post_data['sk']=$taboutus['bdappsecret'];
        $res=$this->httpRequest($url,$post_data);
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>json_decode($res)]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //wechat底部导航栏
    public function tabBar(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $navs=Db::name('nnav')->where('nav_type',1)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
                if($navs){
                    for ($i=0; $i <count($navs) ; $i++) { 
                        $tabs[$i]['selected']='false';
                        $tabs[$i]['text']=$navs[$i]['nav_name'];
                        $tabs[$i]['pagePath']=$navs[$i]['nav_link'];
                        $tabs[$i]['iconPath']=$navs[$i]['nav_ico'];
                        $tabs[$i]['selectedIconPath']=$navs[$i]['imgt'];
                    }
                    $tabs[0]['selected']='true';
                    return json(['code' => 200, 'msg' => '成功！','data'=>$tabs]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $navs=Db::name('nnav')->where('nav_type',1)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
                if($navs){
                    for ($i=0; $i <count($navs) ; $i++) { 
                        $tabs[$i]['selected']='false';
                        $tabs[$i]['text']=$navs[$i]['nav_name'];
                        $tabs[$i]['pagePath']=$navs[$i]['nav_link'];
                        $tabs[$i]['iconPath']=$navs[$i]['nav_ico'];
                        $tabs[$i]['selectedIconPath']=$navs[$i]['imgt'];
                    }
                    $tabs[0]['selected']='true';
                    return json(['code' => 200, 'msg' => '成功！','data'=>$tabs]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //轮播图
    public function pbanner(){
        Db::name('actives')->insert(['cus_id'=>input("param.cus_id"),'time'=>time()]);
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nbanner')->alias('a')->join('nnews b', 'a.link = b.id','LEFT')->field('a.*,b.img,b.title')->where('a.cus_id',$cus_id)->where('a.state',1)->order('sort desc')->select();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nbanner')->alias('a')->join('nnews b', 'a.link = b.id','LEFT')->field('a.*,b.img,b.title')->where('a.cus_id',$cus_id)->where('a.state',1)->order('sort desc')->select();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //公告
    public function pcomad(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(3)->select();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(3)->select();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //分类名
    public function pnewcate(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $cates=Db::name('ncate')->where('type',1)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
                if($cates){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$cates]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $cates=Db::name('ncate')->where('type',1)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
                if($cates){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$cates]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //资讯推荐（所有）
    public function pnews(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('sort desc')->limit(3)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('sort desc')->limit(3)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //资讯推荐（分类名对应的id）
    public function pcatenews(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.cate_id"))){
            $cate_id=input("param.cate_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出分类id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('cate_id',$cate_id)->where('state',1)->where('flag',1)->order('sort desc')->limit(3)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('cate_id',$cate_id)->where('state',1)->where('flag',1)->order('sort desc')->limit(3)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //智能广告
    public function pnad(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nad')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->find();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nad')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->find();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //首页推荐视频
    public function pvideo(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nvideo')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('id desc')->find();
                // $res=Db::name('nvideo')->alias('a')->join('nagree b', 'a.id = b.p_id','LEFT')->field('a.*,count(b.*)')->where('a.cus_id',$cus_id)->where('b.type',2)->where('a.state',1)->where('b.state',1)->where('a.flag',1)->order('a.id desc')->find();
                if($res){
                    $res['agree']=Db::name('nagree')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$res['id'])->count();
                    $res['message']=Db::name('nmessage')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$res['id'])->count();
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nvideo')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('id desc')->find();
                // $res=Db::name('nvideo')->alias('a')->join('nagree b', 'a.id = b.p_id','LEFT')->field('a.*,count(b.*)')->where('a.cus_id',$cus_id)->where('b.type',2)->where('a.state',1)->where('b.state',1)->where('a.flag',1)->order('a.id desc')->find();
                if($res){
                    $res['agree']=Db::name('nagree')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$res['id'])->count();
                    $res['message']=Db::name('nmessage')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$res['id'])->count();
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //浏览资讯
    public function pviewn(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nnews')->where('cus_id',$cus_id)->where('id',$id)->find();
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $update=Db::name('nnews')->where('cus_id',$cus_id)->where('id',$id)->update(['view'=>$res['view']+1]);
                if($update){
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $update=Db::name('nnews')->where('cus_id',$cus_id)->where('id',$id)->update(['view'=>$res['view']+1]);
                if($update){
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }else{
                return json(['code' => 504, 'msg' => '平台被禁用']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }

    }
    //浏览视频
    public function pviewv(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nvideo')->where('cus_id',$cus_id)->where('id',$id)->find();
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $update=Db::name('nvideo')->where('cus_id',$cus_id)->where('id',$id)->update(['view'=>$res['view']+1]);
                if($update){
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $update=Db::name('nvideo')->where('cus_id',$cus_id)->where('id',$id)->update(['view'=>$res['view']+1]);
                if($update){
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }else{
                return json(['code' => 504, 'msg' => '平台被禁用']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }

    }
    //点赞资讯
    public function pagreen(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 505, 'msg' => '请给出openid']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nagree')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',1)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>1]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>1]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=1;
                    $inserts['state']=1;
                    $inserts['time']=time();
                    $insert=Db::name('nagree')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=1;
                    $inserts['state']=1;
                    $inserts['time']=time();
                    $insert=Db::name('nagree')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //取消点赞资讯
    public function pagreencannel(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 506, 'msg' => '请给出openid']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nagree')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',1)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>2]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>2]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                return json(['code' => 505, 'msg' => '未曾点赞过']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //点赞视频
    public function pagreev(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 505, 'msg' => '请给出openid']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nagree')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',2)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>1]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>1]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=2;
                    $inserts['state']=1;
                    $inserts['time']=time();
                    $insert=Db::name('nagree')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=2;
                    $inserts['state']=1;
                    $inserts['time']=time();
                    $insert=Db::name('nagree')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //取消点赞视频
    public function pagreevcannel(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 506, 'msg' => '请给出openid']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nagree')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',2)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>2]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>2]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                return json(['code' => 505, 'msg' => '未曾点赞过']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //点赞留言
    public function pagreem(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 505, 'msg' => '请给出openid']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nagree')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',3)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>1]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>1]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=3;
                    $inserts['state']=1;
                    $inserts['time']=time();
                    $insert=Db::name('nagree')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=3;
                    $inserts['state']=1;
                    $inserts['time']=time();
                    $insert=Db::name('nagree')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //取消点赞留言
    public function pagreemcannel(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 506, 'msg' => '请给出openid']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nagree')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',3)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>2]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $update=Db::name('nagree')->where('id',$res['id'])->update(['state'=>2]);
                    if($update){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                return json(['code' => 505, 'msg' => '未曾点赞过']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //留言资讯
    public function pmessagen(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 505, 'msg' => '请给出openid']);exit();
        }
        if(!empty(input("param.message"))){
            $message=input("param.message");
        }else{
            return json(['code' => 506, 'msg' => '留言呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nmessage')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',1)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    return json(['code' => 201, 'msg' => '留言过了！']);exit();
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    return json(['code' => 201, 'msg' => '留言过了！']);exit();
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=1;
                    $inserts['state']=1;
                    $inserts['message']=$message;
                    $inserts['time']=time();
                    $insert=Db::name('nmessage')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=1;
                    $inserts['state']=1;
                    $inserts['message']=$message;
                    $inserts['time']=time();
                    $insert=Db::name('nmessage')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //留言视频
    public function pmessagev(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出资讯id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 505, 'msg' => '请给出openid']);exit();
        }
        if(!empty(input("param.message"))){
            $message=input("param.message");
        }else{
            return json(['code' => 506, 'msg' => '留言呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $res=Db::name('nmessage')->where('openid',$openid)->where('cus_id',$cus_id)->where('p_id',$id)->where('type',2)->find();
            if($res){
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    return json(['code' => 201, 'msg' => '留言过了！']);exit();
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    return json(['code' => 201, 'msg' => '留言过了！']);exit();
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }else{
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=2;
                    $inserts['state']=1;
                    $inserts['message']=$message;
                    $inserts['time']=time();
                    $insert=Db::name('nmessage')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $inserts['openid']=$openid;
                    $inserts['cus_id']=$cus_id;
                    $inserts['p_id']=$id;
                    $inserts['type']=2;
                    $inserts['state']=1;
                    $inserts['message']=$message;
                    $inserts['time']=time();
                    $insert=Db::name('nmessage')->insert($inserts);
                    if($insert){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败']);exit();
                    }
                }else{
                    return json(['code' => 504, 'msg' => '平台被禁用']);exit();
                }
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
}